import EmailCodeModel from "../models/EmailCodeModel";
import { EmailCodeEntity } from "../entities/EmailCodeEntity";

interface IEmailCodeResult extends EmailCodeEntity {
  createdAt: string,
  updatedAt: string
}

export default class EmailCodeDao {

  static async create(EmailCode: EmailCodeEntity): Promise<object | null> {
    try {
      const result = await EmailCodeModel.create({
        ...EmailCode
      })

      return result?.toJSON() || null

    } catch (err) {

      throw new Error(err.message || err.original.message)
    }

  }

  static async find(EmailCode: EmailCodeEntity): Promise<IEmailCodeResult | null> {
    try {
      const result = await EmailCodeModel.findOne({
        where: {
          ...EmailCode
        }
      })

      return result?.toJSON() || null

    } catch (err) {

      throw new Error(err.message || err.original.message)
    }

  }

}